package cn.edu.huas.shopping.dao;

import cn.edu.huas.shopping.exception.DaoException;
import cn.edu.huas.shopping.pojo.IndexProduct;
import cn.edu.huas.shopping.pojo.OrderItem;
import cn.edu.huas.shopping.pojo.Product;
import cn.edu.huas.shopping.utils.DBUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class ProductDaoImpl implements ProductDao{
    @Override
    public Product queryById(Integer id) throws DaoException {
        String sql="select id,pname,market_price,shop_price,pimage,pdesc,cid from t_product where id=? and pflag='0'";
        return DBUtils.query(sql,rs -> {
            Product p=null;
            if (rs.next()){
                p=new Product();
                p.setId(rs.getInt("id"));
                p.setName(rs.getString("pname"));
                p.setMarketprice(rs.getDouble("market_price"));
                p.setShopPrice(rs.getDouble("shop_price"));
                p.setPimage(rs.getString("pimage"));
                p.setDesc(rs.getString("pdesc"));
                p.setCid(rs.getInt("cid"));
            }
            return p;
        },id);
    }

    @Override
    public List<IndexProduct> queryIndexProduct(String news) throws DaoException {
        String sql="SELECT id,pid,name,price,pimage,pdesc,news,`ORDER` from t_indexproduct where news=? and online='0' ORDER BY `ORDER`";
        return DBUtils.query(sql,(rs)->{
            List<IndexProduct> list=new ArrayList<>();
            while (rs.next()){
                IndexProduct p = new IndexProduct();
                p.setId(rs.getInt("id"));
                p.setPid(rs.getInt("pid"));
                p.setName(rs.getString("name"));
                p.setShopPrice(rs.getDouble("price"));
                p.setPimage(rs.getString("pimage"));
                p.setDesc(rs.getString("pdesc"));
                p.setNews(rs.getString("news"));
                p.setOrder(rs.getInt("order"));
                list.add(p);
            }
            return list;
        },news);
    }

    @Override
    public OrderItem queryOrderItem(Integer id) throws DaoException {
        String sql="select * from t_orderitem where id=?";
        return DBUtils.query(sql,rs -> {
            OrderItem p=null;
            if (rs.next()){
                p=new OrderItem();
                p.setId(rs.getInt("id"));
                p.setPname(rs.getString("pname"));
                p.setPrice(rs.getDouble("price"));
                p.setQty(rs.getInt("qty"));
            }
            return p;
        },id);
    }

    @Override
    public int queryCountByCid(Integer cid) throws DaoException {
        final String sql="select count(id) from t_product where cid=?";
        return DBUtils.query(sql,rs -> {
            int count=-1;
            if (rs.next()){
                count= rs.getInt(1);
            }
           return count;
        },cid);
    }

    @Override
    public List<Product> queryProductsByCid(Integer cid, Integer beginIndex, Integer size) {
        final String sql = "select id,pname,shop_price,pimage,pdesc,cid from t_product where cid=? limit ?,?";
        Object[] args = {cid, beginIndex, size};
        return DBUtils.query(sql, rs -> {
            List<Product> datas = new ArrayList<>();
            while (rs.next()) {
                Product p = new Product();
                p.setId(rs.getInt("id"));
                p.setName(rs.getString("pname"));
                p.setShopPrice(rs.getDouble("shop_price"));
                p.setPimage(rs.getString("pimage"));
                p.setDesc(rs.getString("pdesc"));
                p.setCid(rs.getInt("cid"));
                datas.add(p);
            }
            return datas;
        }, args);
    }
}
